package com.gy.bearhelps.handle;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.gy.bearhelps.R;
import com.gy.bearhelps.base.BaseActivity;
import com.gy.bearhelps.base.BaseBean;
import com.gy.bearhelps.handle.adapter.HandleDetailContentAdapter;
import com.gy.bearhelps.handle.adapter.HandleDetailFileAdapter;
import com.gy.bearhelps.handle.bean.HandleDetailBean;
import com.gy.bearhelps.util.AndroidFileUtil;
import com.gy.bearhelps.util.LogUtil;
import com.gy.bearhelps.util.RequestResultListener;
import com.gy.bearhelps.util.Urls;
import com.gy.bearhelps.view.MyFullListView;
import com.nostra13.universalimageloader.utils.StorageUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2016/11/9.
 * Describe:办理流程详情
 */
public class HandleDetailActivity extends BaseActivity {

    private MyFullListView listViewContent, listViewDown;
    private HandleDetailContentAdapter contentAdapter;
    private HandleDetailFileAdapter fileAdapter;
    private HandleDetailBean detailBean;
    private List<HandleDetailBean.ListBean> listContent;
    private List<HandleDetailBean.FileListBean> listFiles;
    private int pid;
    private String path;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_handle_detail);
        setTitleMsg("办事指南");
        listViewContent = (MyFullListView) findViewById(R.id.activity_handle_list_content);
        listViewDown = (MyFullListView) findViewById(R.id.activity_handle_list_down);
    }

    @Override
    protected void initData() {
        pid = getIntent().getIntExtra("pid", 0);
        listContent = new ArrayList<>();
        listFiles = new ArrayList<>();
        contentAdapter = new HandleDetailContentAdapter(this, listContent);
        fileAdapter = new HandleDetailFileAdapter(this, listFiles);
        path = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "bearhelp").getAbsolutePath();
    }

    @Override
    protected void setListener() {
        super.setListener();
        listViewDown.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogUtil.e(position + "-----");
                downFile(listFiles.get(position).fileUrl, listFiles.get(position).fileName);
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listViewContent.setAdapter(contentAdapter);
        listViewDown.setAdapter(fileAdapter);
        getDetail();
    }

    /**
     * 请求详情
     */
    private void getDetail() {
        showLoadDialog();
        RequestParams params = new RequestParams(Urls.handle_detail);
        params.addQueryStringParameter("pid",pid+"");
        getRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                try {
                    JSONArray array = new JSONArray(result.data);
                    detailBean = gson.fromJson(array.getString(0), HandleDetailBean.class);
//                    setTitleMsg(detailBean.entry.title);
                    listContent.clear();
                    listFiles.clear();
                    listContent.addAll(detailBean.list);
                    listFiles.addAll(detailBean.fileList);
                    contentAdapter.notifyDataSetChanged();
                    fileAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }

    private void downFile(String url, String name) {
        showLoadDialog("下载中...");
        RequestParams params = new RequestParams(url);
        path = path + "/" + name;
        params.setSaveFilePath(path);
        x.http().get(params, new Callback.CommonCallback<File>() {
            @Override
            public void onSuccess(File result) {
                closeLoadDialog();
                showToast("下载成功");
                Intent intent = AndroidFileUtil.openFile(result.getAbsolutePath());
                if(intent != null){
                    startActivity(intent);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeLoadDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                closeLoadDialog();
            }

            @Override
            public void onFinished() {
                closeLoadDialog();
            }
        });
    }
}
